package com.timmy.chi.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.timmy.chi.R;
import com.timmy.chi.model.beans.UserOrder;
import com.timmy.chi.shopcar.util.Utils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

public class OrderAdapter extends BaseAdapter {


    private static final String TAG = "OrderAdapter";
    private final Context mContext;

    private List<UserOrder.OrdersBean> mList = new ArrayList<>();

    public OrderAdapter(Context context) {
        mContext = context;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public UserOrder.OrdersBean getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    public void setList(List<UserOrder.OrdersBean> list) {
        mList.clear();
        mList.addAll(list);

        // 反转集合
        Collections.reverse(mList);
        notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        final Viewholder viewholder;

        if (convertView == null) {
            view = LayoutInflater.from(mContext).inflate(R.layout.order_item, null);
            viewholder = new Viewholder();

            viewholder.tv_time = (TextView) view.findViewById(R.id.order_time);
            viewholder.tv_state = (TextView) view.findViewById(R.id.order_state);
            viewholder.tv_name = (TextView) view.findViewById(R.id.order_name);
            viewholder.tv_price = (TextView) view.findViewById(R.id.order_price);

            view.setTag(viewholder);

        } else {
            viewholder = (Viewholder) view.getTag();
        }


        UserOrder.OrdersBean data = mList.get(position);

        // 时间
        long time = data.getOrder().getTime();
        Date date = new Date(time);
        String datatime = Utils.fDateCNYRSF(date);

        // 价格
        float prices = 0;

        // 状态
        int state = 0;

        boolean state_ = false;

        // 该订单的所有商品
        List<UserOrder.OrdersBean.FoodStatesBean> foodStates = data.getFoodStates();

        if (foodStates.size() == 0 ){
            mList.remove(position);
            notifyDataSetChanged();

            return view;
        }

        String name = foodStates.get(0).getFood().getName();

        for (UserOrder.OrdersBean.FoodStatesBean foodState : foodStates) {
            float price = (float) foodState.getFood().getPrice();
            int count = foodState.getCount();
            float v = price * count;
            prices += v;

            // 商品状态
            int sid = foodState.getSid();

            if (sid == 3) {
                state += 3;
            } else if (sid == 2) {
                state++;
            } else if (sid == 1) {
                state_ = true;
            }
        }

        int size = foodStates.size();

        if (size == 1) {
            viewholder.tv_name.setText(name);
        } else {
            viewholder.tv_name.setText(name + "等" + size + "件商品");
        }

        viewholder.tv_price.setText(prices + "");
        viewholder.tv_time.setText(datatime);

        if (state == (size * 3)) {
            viewholder.tv_state.setText("订单已撤销");
        } else {
            viewholder.tv_state.setText("订单已完成");
        }

        if (state_) {
            viewholder.tv_state.setText("订单尚未完成");
        }
        return view;
    }

    class Viewholder {
        TextView tv_state;
        TextView tv_name;
        TextView tv_time;
        TextView tv_price;
    }


}
